package ldkxingzhe.l365calendar.widget.dialog.picker;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;

import ldkxingzhe.l365calendar.R;
import ldkxingzhe.l365calendar.utils.OSUtil;
import ldkxingzhe.l365calendar.widget.wheelview.WheelView;
import ldkxingzhe.l365calendar.widget.wheelview.adapter.NumericWheelAdapter;

/**
 * 时间拾取器
 * Created by xingzhe on 16/1/28.
 */
public class DateTimePicker extends DatePicker{
    @SuppressWarnings("unused")
    private static final String TAG = "DateTimePicker";


    private WheelView hour, minute;
    private NumericWheelAdapter hourAdapter, minuteAdapter;

    public static DateTimePicker newInstance(long timeLong) {
        if (timeLong <= 0) timeLong = new Date().getTime();
        Bundle args = new Bundle();
        args.putLong(TIME, timeLong);
        DateTimePicker fragment = new DateTimePicker();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View resultView = inflater.inflate(R.layout.picker_time_layout, container, false);
        year = (WheelView) resultView.findViewById(R.id.year);
        month = (WheelView) resultView.findViewById(R.id.month);
        day = (WheelView) resultView.findViewById(R.id.day);
        hour = (WheelView) resultView.findViewById(R.id.hour);
        minute = (WheelView) resultView.findViewById(R.id.minute);
        title = (TextView) resultView.findViewById(R.id.title);
        initYMDAdapter();
        initHMAdapter();
        initButtonLayout(resultView.findViewById(R.id.button_layout));
        if(time != null){
            initYMD();
            initHM();
        }
        return resultView;
    }

    @Override
    protected String getResult() {
        DecimalFormat format = new DecimalFormat("00");
        return super.getResult() + " " + format.format(getHour()) + "-" + format.format(getMinute());
    }

    private int getHour(){
        return hour.getCurrentItem();
    }

    private int getMinute(){
        return minute.getCurrentItem();
    }

    private void initHMAdapter() {
        hourAdapter = new NumericWheelAdapter(getActivity(), 0, 59);
        minuteAdapter = new NumericWheelAdapter(getActivity(), 0, 59);
        hourAdapter.setLabel("");
        minuteAdapter.setLabel("");
        setTextSizeAndColor(hourAdapter);
        setTextSizeAndColor(minuteAdapter);
        hour.setViewAdapter(hourAdapter);
        minute.setViewAdapter(minuteAdapter);
        hour.setCyclic(true);
        minute.setCyclic(true);
    }

    @Override
    protected void setTextSizeAndColor(NumericWheelAdapter adapter) {
        adapter.setTextSize((int) OSUtil.dp2px(7));
        adapter.setTextColor(Color.BLACK);
    }

    private void initHM() {
        hour.setCurrentItem(time.get(Calendar.HOUR_OF_DAY));
        minute.setCurrentItem(time.get(Calendar.MINUTE));
    }
}
